package com.jyh.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyh.blog.dao.PhotoDao;
import com.jyh.blog.dto.AlbumPhotoDTO;
import com.jyh.blog.dto.PhotoBackDTO;
import com.jyh.blog.entity.Album;
import com.jyh.blog.entity.Photo;
import com.jyh.blog.exception.MyException;
import com.jyh.blog.service.AlbumService;
import com.jyh.blog.service.PhotoService;
import com.jyh.blog.utils.BeanCopyUtils;
import com.jyh.blog.utils.PageUtils;
import com.jyh.blog.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.jyh.blog.constant.CommonConst.FALSE;
import static com.jyh.blog.enums.ArticleStatusEnum.PUBLIC;

/**
 * (Photo)表服务实现类
 *
 * @author jyh
 * @since 2023-02-12 21:29:19
 */
@Service("photoService")
public class PhotoServiceImpl extends ServiceImpl<PhotoDao, Photo> implements PhotoService {
    @Autowired
    private AlbumService albumService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void savePhoto(PhotoVO photoVO) {
        List<String> urlList = photoVO.getPhotoUrlList();
        List<Photo> photos = urlList.stream().map(url -> Photo.builder()
                .albumId(photoVO.getAlbumId())
                .address(url)
                .name(IdWorker.getIdStr())
                .build())
                .collect(Collectors.toList());
        this.saveBatch(photos);
    }

    @Override
    public PageResult<PhotoBackDTO> photoList(ConditionVO conditionVO) {
        Page<Photo> page = new Page<>(PageUtils.getLimitCurrent(), PageUtils.getSize());
        this.page(page, new LambdaQueryWrapper<Photo>()
                .eq(!StringUtils.isBlank(conditionVO.getAlbumId()), Photo::getAlbumId, conditionVO.getAlbumId())
                .eq(Photo::getIsDelete,conditionVO.getIsDelete())
        );
        List<PhotoBackDTO> photoBackDTOS = BeanCopyUtils.copyList(page.getRecords(), PhotoBackDTO.class);
        return new PageResult<>(photoBackDTOS, (int) page.getTotal());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void logicDelete(DeleteVO deleteVO) {
        //deleteVO.getIsDelete() 返回1 说明删除，0说明恢复
        List<Photo> photoList = deleteVO.getIdList().stream().map(id -> Photo.builder()
                .id(id)
                .isDelete(deleteVO.getIsDelete())
                .build())
                .collect(Collectors.toList());
        this.updateBatchById(photoList);
        //相册、图片都为已删除，进行恢复操作，恢复图片后默认恢复对应相册
        if (deleteVO.getIsDelete().equals(FALSE)) {
            List<Album> albums = this.list(new LambdaQueryWrapper<Photo>()
                    .select(Photo::getAlbumId)
                    .in(Photo::getId, deleteVO.getIdList())
                    .groupBy(Photo::getAlbumId))
                    .stream().map(item -> Album.builder()
                            .id(item.getAlbumId())
                            .is_delete(FALSE)
                            .build())
                    .collect(Collectors.toList());
            albumService.updateBatchById(albums);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void movePhoto(PhotoVO photoVO) {
        List<Photo> photoList = photoVO.getPhotoIdList().stream().map(id -> Photo.builder()
                .id(id)
                .albumId(photoVO.getAlbumId())
                .build())
                .collect(Collectors.toList());
        this.updateBatchById(photoList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deletePhotos(List<String> photoIdList) {
        this.removeByIds(photoIdList);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void uploadPhoto(PhotoInfoVO photoInfoVo) {
        Photo photo = BeanCopyUtils.copyObject(photoInfoVo, Photo.class);
        this.updateById(photo);
    }

    @Override
    public AlbumPhotoDTO photoOfAlbumList(String id) {
        //查询相册
        Album album = albumService.getOne(new LambdaQueryWrapper<Album>()
                .select(Album::getCover, Album::getName)
                .eq(Album::getId, id)
                .eq(Album::getStatus, PUBLIC.getStatus())
                .eq(Album::getIs_delete, FALSE)
        );
        if(Objects.isNull(album)){
            throw new MyException("相册不存在");
        }
        //查询相册中的图片
        Page<Photo> page = new Page<>(PageUtils.getLimitCurrent(), PageUtils.getSize());
        List<String> photoAddressList = this.page(page,new LambdaQueryWrapper<Photo>()
                .select(Photo::getAddress)
                .eq(Photo::getAlbumId, id)
                .eq(Photo::getIsDelete, FALSE)
                .orderByDesc(Photo::getId))
                .getRecords()
                .stream()
                .map(Photo::getAddress)
                .collect(Collectors.toList());
        //封装
        return AlbumPhotoDTO.builder()
                .name(album.getName())
                .cover(album.getCover())
                .photos(photoAddressList)
                .build();
    }
}

